package utils

import (
	"fmt"
	"strings"
	"time"
)

func Utc2Local(utc_text string) (local string){
	t,_ := time.Parse(time.RFC3339, utc_text)
	s := t.In(time.Local)
	local = s.Format("2006-01-02 15:04:05")
	return
}


func Utc2IterationName(utc_text string) (name string){
	t,_ := time.Parse(time.RFC3339, utc_text)
	s := t.In(time.Local)
	local := s.Format("2006-01-02")
	localList := strings.SplitN(local, "-",2)  //[2006 01-02]
	name = fmt.Sprintf("%s版本_%s", localList[1], localList[0])
	return
}

const DATE_FORMAT = "2006-01-02"

func GetFirstDateOfWeek() (weekMonday string) {
	now := time.Now()
	offset := int(time.Monday - now.Weekday())
	if offset > 0 {
		offset = -6
	}
	weekStartDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
	weekMonday = weekStartDate.Format(DATE_FORMAT)
	return
}

//获取上周一到周日的日期
func GetLastWeek()(dataRange string){
	thisWeekMonday := GetFirstDateOfWeek()
	TimeMonday, _ := time.Parse("2006-01-02", thisWeekMonday)
	lastWeekMonday := TimeMonday.AddDate(0, 0, -7)
	lastWeekSunday := TimeMonday.AddDate(0, 0, -1)
	dataRange = fmt.Sprintf("%s_%s", lastWeekMonday.Format(DATE_FORMAT), lastWeekSunday.Format(DATE_FORMAT))
	return
}

func GetLastTowWeek()(dataRange string){
	thisWeekMonday := GetFirstDateOfWeek()
	TimeMonday, _ := time.Parse("2006-01-02", thisWeekMonday)
	lastWeekMonday := TimeMonday.AddDate(0, 0, -14)
	lastWeekSunday := TimeMonday.AddDate(0, 0, -8)
	dataRange = fmt.Sprintf("%s_%s", lastWeekMonday.Format(DATE_FORMAT), lastWeekSunday.Format(DATE_FORMAT))
	return
}

func GetLastThreeWeek()(dataRange string){
	thisWeekMonday := GetFirstDateOfWeek()
	TimeMonday, _ := time.Parse("2006-01-02", thisWeekMonday)
	lastWeekMonday := TimeMonday.AddDate(0, 0, -21)
	lastWeekSunday := TimeMonday.AddDate(0, 0, -15)
	dataRange = fmt.Sprintf("%s_%s", lastWeekMonday.Format(DATE_FORMAT), lastWeekSunday.Format(DATE_FORMAT))
	return
}

//获取上个月月初到月末的日期
func GetLastMonth()(dataRange string){
	year, month, _ := time.Now().Date()
	thisMonth := time.Date(year, month, 1, 0, 0, 0, 0, time.Local)
	start := thisMonth.AddDate(0, -1, 0).Format(DATE_FORMAT)
	end := thisMonth.AddDate(0, 0, -1).Format(DATE_FORMAT)
	dataRange = fmt.Sprintf("%s_%s", start, end)
	return
}

func GetLastTowMonth()(dataRange string){
	year, month, _ := time.Now().Date()
	thisMonth := time.Date(year, month, 1, 0, 0, 0, 0, time.Local)
	start := thisMonth.AddDate(0, -2, 0).Format(DATE_FORMAT)
	end := thisMonth.AddDate(0, 0, -2).Format(DATE_FORMAT)
	dataRange = fmt.Sprintf("%s_%s", start, end)
	return
}

//获取上个月月初到月末的日期
func GetLastThreeMonth()(dataRange string){
	year, month, _ := time.Now().Date()
	thisMonth := time.Date(year, month, 1, 0, 0, 0, 0, time.Local)
	start := thisMonth.AddDate(0, -3, 0).Format(DATE_FORMAT)
	end := thisMonth.AddDate(0, 0, -3).Format(DATE_FORMAT)
	dataRange = fmt.Sprintf("%s_%s", start, end)
	return
}